\(\sum{(Px(value==1)*px\_resolution ^2 )}\)

library(pixmap)
library(magrittr)
library(EBImage)
library(image.LineSegmentDetector)
library(image.ContourDetector)
library(image.CannyEdges)

setwd("~/Zellkraftwerk/packages/RJobTissueArea")

file <- file.path("inst/data","data_sum_M1730408_pos11.csv")

data.sum <- file%>%
  data.table::fread()

m.data_sum <- data.sum%>%
  as.matrix()

#cellres: pixel resolution in horizontal and vertical direction

image <-pixmapGrey(m.data_sum,cellres=c(1,1))

plot(image)

EBImage

constant threshold

grid <- expand.grid(
  sigma = seq(0.5,3,0.4),
  threshold = seq(2,4,0.25)
)
grid$min <- NA
grid$max <- NA

img2 <- image@grey * 255
# Set up plots for 96 dpi images
  #library(EBImage)
  #dm <- dim(img2)/96
  #dev.new(width = dm[1], height = dm[2])
# Low pass filter with gblur and make binary

  par(mfrow=c(1,2))
  
for(i in 1:dim(grid)[1]){

    writeLines(c(
    paste0("- sigma: ",grid$sigma[i]),
    paste0("- threshold: ", grid$threshold[i])
  ))

  #Low-pass Gaussian filter
  xb <- EBImage::gblur(img2, grid$sigma[i])
  xb <- round(xb,digits = 0)
  #plot(xb)

  image2 <- pixmapGrey(xb,cellres=c(1,1)) #(xb-min(xb))/(max(xb)-min(xb))

  plot(image2)

  grid$min[i] <- min(xb)
  grid$max[i] <- max(xb)
  
  #threshold filtering
  pos <- which(xb > grid$threshold[i])
  xt <- xb
  xt[which(xb > grid$threshold[i])] <- 1
  xt[which(xb <= grid$threshold[i])] <- 0
  
 #xt <- EBImage::thresh(xb, w=grid$w[i],
 #                      h=grid$w[i],
 #                      offset = grid$offset[i])
 #plot(xt)

 image3 <- image
 image3@grey <- xt

 plot(image3)
}
## - sigma: 0.5
## - threshold: 2

## - sigma: 0.9
## - threshold: 2

## - sigma: 1.3
## - threshold: 2

## - sigma: 1.7
## - threshold: 2

## - sigma: 2.1
## - threshold: 2

## - sigma: 2.5
## - threshold: 2

## - sigma: 2.9
## - threshold: 2

## - sigma: 0.5
## - threshold: 2.25

## - sigma: 0.9
## - threshold: 2.25

## - sigma: 1.3
## - threshold: 2.25

## - sigma: 1.7
## - threshold: 2.25

## - sigma: 2.1
## - threshold: 2.25

## - sigma: 2.5
## - threshold: 2.25

## - sigma: 2.9
## - threshold: 2.25

## - sigma: 0.5
## - threshold: 2.5

## - sigma: 0.9
## - threshold: 2.5

## - sigma: 1.3
## - threshold: 2.5

## - sigma: 1.7
## - threshold: 2.5

## - sigma: 2.1
## - threshold: 2.5

## - sigma: 2.5
## - threshold: 2.5

## - sigma: 2.9
## - threshold: 2.5

## - sigma: 0.5
## - threshold: 2.75

## - sigma: 0.9
## - threshold: 2.75

## - sigma: 1.3
## - threshold: 2.75

## - sigma: 1.7
## - threshold: 2.75

## - sigma: 2.1
## - threshold: 2.75

## - sigma: 2.5
## - threshold: 2.75

## - sigma: 2.9
## - threshold: 2.75

## - sigma: 0.5
## - threshold: 3

## - sigma: 0.9
## - threshold: 3

## - sigma: 1.3
## - threshold: 3

## - sigma: 1.7
## - threshold: 3

## - sigma: 2.1
## - threshold: 3

## - sigma: 2.5
## - threshold: 3

## - sigma: 2.9
## - threshold: 3

## - sigma: 0.5
## - threshold: 3.25

## - sigma: 0.9
## - threshold: 3.25

## - sigma: 1.3
## - threshold: 3.25

## - sigma: 1.7
## - threshold: 3.25

## - sigma: 2.1
## - threshold: 3.25

## - sigma: 2.5
## - threshold: 3.25

## - sigma: 2.9
## - threshold: 3.25

## - sigma: 0.5
## - threshold: 3.5

## - sigma: 0.9
## - threshold: 3.5

## - sigma: 1.3
## - threshold: 3.5

## - sigma: 1.7
## - threshold: 3.5

## - sigma: 2.1
## - threshold: 3.5

## - sigma: 2.5
## - threshold: 3.5

## - sigma: 2.9
## - threshold: 3.5

## - sigma: 0.5
## - threshold: 3.75

## - sigma: 0.9
## - threshold: 3.75

## - sigma: 1.3
## - threshold: 3.75

## - sigma: 1.7
## - threshold: 3.75

## - sigma: 2.1
## - threshold: 3.75

## - sigma: 2.5
## - threshold: 3.75

## - sigma: 2.9
## - threshold: 3.75

## - sigma: 0.5
## - threshold: 4

## - sigma: 0.9
## - threshold: 4

## - sigma: 1.3
## - threshold: 4

## - sigma: 1.7
## - threshold: 4

## - sigma: 2.1
## - threshold: 4

## - sigma: 2.5
## - threshold: 4

## - sigma: 2.9
## - threshold: 4

grid
##    sigma threshold min max
## 1    0.5      2.00   0 203
## 2    0.9      2.00   0 201
## 3    1.3      2.00   0 194
## 4    1.7      2.00   0 185
## 5    2.1      2.00   0 174
## 6    2.5      2.00   0 163
## 7    2.9      2.00   0 151
## 8    0.5      2.25   0 203
## 9    0.9      2.25   0 201
## 10   1.3      2.25   0 194
## 11   1.7      2.25   0 185
## 12   2.1      2.25   0 174
## 13   2.5      2.25   0 163
## 14   2.9      2.25   0 151
## 15   0.5      2.50   0 203
## 16   0.9      2.50   0 201
## 17   1.3      2.50   0 194
## 18   1.7      2.50   0 185
## 19   2.1      2.50   0 174
## 20   2.5      2.50   0 163
## 21   2.9      2.50   0 151
## 22   0.5      2.75   0 203
## 23   0.9      2.75   0 201
## 24   1.3      2.75   0 194
## 25   1.7      2.75   0 185
## 26   2.1      2.75   0 174
## 27   2.5      2.75   0 163
## 28   2.9      2.75   0 151
## 29   0.5      3.00   0 203
## 30   0.9      3.00   0 201
## 31   1.3      3.00   0 194
## 32   1.7      3.00   0 185
## 33   2.1      3.00   0 174
## 34   2.5      3.00   0 163
## 35   2.9      3.00   0 151
## 36   0.5      3.25   0 203
## 37   0.9      3.25   0 201
## 38   1.3      3.25   0 194
## 39   1.7      3.25   0 185
## 40   2.1      3.25   0 174
## 41   2.5      3.25   0 163
## 42   2.9      3.25   0 151
## 43   0.5      3.50   0 203
## 44   0.9      3.50   0 201
## 45   1.3      3.50   0 194
## 46   1.7      3.50   0 185
## 47   2.1      3.50   0 174
## 48   2.5      3.50   0 163
## 49   2.9      3.50   0 151
## 50   0.5      3.75   0 203
## 51   0.9      3.75   0 201
## 52   1.3      3.75   0 194
## 53   1.7      3.75   0 185
## 54   2.1      3.75   0 174
## 55   2.5      3.75   0 163
## 56   2.9      3.75   0 151
## 57   0.5      4.00   0 203
## 58   0.9      4.00   0 201
## 59   1.3      4.00   0 194
## 60   1.7      4.00   0 185
## 61   2.1      4.00   0 174
## 62   2.5      4.00   0 163
## 63   2.9      4.00   0 151
write.csv(grid,"grid_resulttable.csv")

supplement

adapting threshold

grid <- expand.grid(sigma = c(1,2,3,4,5,10,100),
             offset = c(100,10,1,0,0.01,0.001,0.0001,0.00001,0.000001,0.0000001),
             w=c(1,5,10,50))

img2 <- image@grey * 255
# Set up plots for 96 dpi images
  #library(EBImage)
  #dm <- dim(img2)/96
  #dev.new(width = dm[1], height = dm[2])
# Low pass filter with gblur and make binary

for(i in 1:dim(grid)[1]){

    writeLines(c(
    paste0("- sigma: ",grid$sigma[i]),
    paste0("- offset: ", grid$offset[i]),
    paste0(" - w and h: ",grid$w[i])
  ))

  #Low-pass Gaussian filter
  xb <- EBImage::gblur(img2, grid$sigma[i])
  plot(xb)

  image2 <- pixmapGrey(xb,cellres=c(1,1)) #(xb-min(xb))/(max(xb)-min(xb))

  plot(image2)


  xt <- EBImage::thresh(xb, w=grid$w[i],
                        h=grid$w[i],
                        offset = grid$offset[i])
  plot(xt)

  image3 <- image
  image3@grey <- xt

  plot(image3)
}

image_line_segment_detector

x <- image@grey * 255

linesegments <- image_line_segment_detector(x,
                                            scale = 0.8,
                                            sigma_scale = 0.6,
                                            quant = 2,
                                            ang_th = 22.5,
                                            log_eps = 0,
                                            density_th = 0.7,
                                            n_bins = 1024,
                                            union = FALSE,
                                            union_min_length = 5,
                                            union_max_distance = 5,
                                            union_ang_th = 7,
                                            union_use_NFA = FALSE,
                                            union_log_eps = 0)

linesegments
plot(image)
plot(linesegments, add = TRUE, col = "red")

image_contour_detector

contourlines  <- image_contour_detector(x, Q = 2)
contourlines
plot(image)
plot(contourlines, add = TRUE, col = "red")

image_canny_edge_detector

edges <- image_canny_edge_detector(x)
edges
plot(edges)